package com.ziyun.pop.web.modules.purchase.controller;

import com.utility.http.HttpUtils;
import com.ziyun.pop.common.utils.JsonUtils;
import com.ziyun.pop.common.utils.UserUtils;
import com.ziyun.pop.modules.purchase.entity.StorePurchaseApplyDetailEntity;
import com.ziyun.pop.modules.purchase.entity.StorePurchaseApplyEntity;
import com.ziyun.pop.modules.purchase.entity.dto.StorePurchaseApplyDto;
import com.ziyun.pop.modules.storeMaterials.service.ErpStockService;
import com.ziyun.pop.modules.storeMaterials.service.StoreMaterialInOutService;
import com.ziyun.pop.modules.storeMaterials.service.StorePurchaseApplyDetailService;
import com.ziyun.pop.modules.storeMaterials.service.StoreStockMaterialService;
import com.ziyun.pop.modules.supp.entity.SellerInfoEntity;
import io.renren.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * 门店采购申请表
 *
 * @author guizhao
 * @email 
 * @date 2018-09-26 13:28:47
 */
@RestController
@RequestMapping("purchase/storepurchaseapply")
public class StorePurchaseApplyController {

    @Autowired
    private StoreStockMaterialService storeStockMaterialService;

    @Autowired
    private StoreMaterialInOutService storeMaterialInOutService;

    @Autowired
    private StorePurchaseApplyDetailService storePurchaseApplyDetailService;

    @Autowired
    private ErpStockService erpStockService;

    @Value("${erpApi}")
    private String erpApi ;

    /**
     * 列表
     */
    @RequestMapping("/querylistPage")
    public R querylistPage(@RequestBody Map<String,Map<String,Object>> params){
        params.get("params").put("sellerId",UserUtils.getCurentUser().getSellerId());
		String url = erpApi + "/purchase/storepurchaseapply/querylistPage";
        String response =  HttpUtils.doPost(url,params);
        R rs = JsonUtils.string2Object(response, R.class);
        return rs;
    }
    /**
     * @author   Alix_zw
     * @Date
     * @Description   获取门店信息
    **/
    @RequestMapping("/getMD")
    public R getMD(){
        List<SellerInfoEntity> selectList = storeMaterialInOutService.getSelectList();
        Map<String,Object> userData = new HashMap<>();
        userData.put("selectList",selectList);
        return R.ok().put("userData",userData);
    }

    /**
     * @author   Alix_zw
     * @Date
     * @Description   获取采购申请明细
     **/


    @RequestMapping("/querylistDetail/{applyNo}")
    public R querylistDetail(@PathVariable("applyNo") String applyNo){

        List<StorePurchaseApplyDetailEntity> materialByinNo = storePurchaseApplyDetailService.getMaterialByApplyNo(applyNo);
        return R.ok().put("materialByinNo",materialByinNo);
    }

    /**
     * 获取枚举类信息
     */
    @RequestMapping("/getEnumList")
    public R getEnumList(){

        String url = erpApi + "/purchase/storepurchaseapply/getEnumList";

        String response =  HttpUtils.doPost(url,null);
        R rs = JsonUtils.string2Object(response, R.class);
        return rs;
    }

    /**
     * 获取耗材明细列表
     */
    @RequestMapping("/queryMaterialsList")
    public R queryMaterialsList(@RequestBody Map<String,Object> params) {
        Integer stockId = erpStockService.getStockId(UserUtils.getCurentUser().getSellerId());
        String url = erpApi + "/purchase/storepurchaseapply/queryMaterialsList";
        Map params1 = (Map) params.get("params");
        params1.put("stockId",stockId);
        String response =  HttpUtils.doPost(url,params);
        R rs = JsonUtils.string2Object(response, R.class);
        return rs;
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody StorePurchaseApplyDto storePurchaseApplyDto){

        storePurchaseApplyDto.getStorePurchaseApplyEntity().setSellerId(UserUtils.getCurentUser().getSellerId().intValue());

        StorePurchaseApplyEntity storePurchaseApplyEntity = storePurchaseApplyDto.getStorePurchaseApplyEntity();

        storePurchaseApplyEntity.setCreateName(UserUtils.getCurentUser().getUsername());

        storePurchaseApplyDto.setStorePurchaseApplyEntity(storePurchaseApplyEntity);

        String url = erpApi + "/purchase/storepurchaseapply/save";
        // 返回
        String response =  HttpUtils.doPost(url,storePurchaseApplyDto);

        R rs = JsonUtils.string2Object(response, R.class);
        return  rs;
    }


    /**
     * @author   Alix_zw
     * @Date
     * @Description   提交申请的时候验证MaterialID在StoreStockMaterial 是否维护单价
     
     * @Return    
    **/

    @RequestMapping("/selectCostByMaterialId/{materialId}")
    public R querySelectCostByMaterialId(@PathVariable("materialId") String materialId){
        R r = storeStockMaterialService.selectCostByMaterialId(materialId);
        return r;
    }

    /**
     * 采购申请修改
     */

    @RequestMapping("/update")
    public R update(@RequestBody StorePurchaseApplyDto storePurchaseApplyDto){

        storePurchaseApplyDto.getStorePurchaseApplyEntity().setSellerId(UserUtils.getCurentUser().getSellerId().intValue());

        StorePurchaseApplyEntity storePurchaseApplyEntity = storePurchaseApplyDto.getStorePurchaseApplyEntity();

        storePurchaseApplyEntity.setCreateName(UserUtils.getCurentUser().getUsername());

        storePurchaseApplyDto.setStorePurchaseApplyEntity(storePurchaseApplyEntity);

        storePurchaseApplyDetailService.updateDetail(storePurchaseApplyDto);

        return  R.ok();
    }

    /**
     * 采购明细
     */
    @RequestMapping("/queryDetailList/{applyId}")
    public R queryDetailList(@PathVariable("applyId") Integer applyId){
        String url = erpApi + "/purchase/storepurchaseapply/queryList/"+applyId;

        String response =  HttpUtils.doPost(url,null);
        R rs = JsonUtils.string2Object(response, R.class);
        return rs;
    }


}
